﻿(function () {
    $(function () {
        var _organizationUnitService = abp.services.app.organizationUnit;
        var _$modal = $("#OrgCreateModal");
        var _$form = _$modal.find("form[name=orgCreateForm]");

        var _$modaledit = $("#OrgEditModal");
        var _$formedit = _$modaledit.find("form[name=orgEditForm]");
        //添加部门
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }
            var org = _$form.serializeFormToObject();
            org.userId = null;
            abp.ui.setBusy(_$modal);
            //新建
                _organizationUnitService.createOrganizationUnit(org).done(function (data) {
                    console.log(data);
                    _$modal.modal("hide");
                    location.reload(true);
                }).always(function () {
                    abp.ui.clearBusy(_$modal);
                });
        });

        //编辑部门信息后提交保存
        _$formedit.find('button[type=submit]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }
            var org = _$formedit.serializeFormToObject();

            abp.ui.setBusy(_$modal);
            if (org.id !== "") {
                //修改
                _organizationUnitService.updateOrganizationUnit(org).done(function () {
                    _$modal.modal('hide');
                    location.reload(true); //reload page to see edited user!
                }).always(function () {
                    abp.ui.clearBusy(_$modal);
                });
            }          
        });

        //刷新页面
        $("#RefreshButton").click(function () {
            refreshOrg()
        });
        function refreshOrg() {
            location.reload();
        }
        //删除
        $(".delete-org").click(function () {
          

            var orgId = $(this).attr("data-org-id");
            var orgName = $(this).attr("data-org-name");
           // alert(orgId + orgName);
            deleteOrg(orgId, orgName);
        });

        function deleteOrg(orgId, orgName) {
           
                abp.message.confirm(
                    abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DualControl'), orgName),
                    function (isConfirmed) {
                        if (isConfirmed) {
                            _organizationUnitService.deleteOrganizationUnit({
                                id: orgId
                            }).done(function () {
                                refreshOrg();
                            });
                        }
                    }
                );
        }
        //打开编辑部门信息模态窗口
        $(".edit-org").click(function (e) {
            e.preventDefault();
            var orgid = $(this).attr("data-org-id");

            //用户的Id
            var userid = $(this).attr("data-user-id");          

            //查询部门信息赋值
            _organizationUnitService.getOrganizationUnitById(orgid).done(function (data) {
                $("input[name=id]").val(data.id);
                $("input[name=DisplayName]").val(data.displayName).parent().addClass("focused");

                //$("#UserId").val(data.userId);

                //$("#div-user").find("#UserId").selectpicker('refresh');
            });

            //清空原有的选项，否则每次打开会增加
            $("option", _$formedit.find("#UserId")).remove(); 

            //查询部门所属职工信息
            _organizationUnitService.getAllOrganizationUnitUsers(orgid).done(function (data) {

                //alert(data.items[0].name + "编号：" + data.items[0].id);

                var userdropdownlist = _$formedit.find("#UserId");

                //追加一个空选项
                userdropdownlist.append("<option value=''>" + "请选择部门负责人" + "</option>"); 

                for (var i = 0; i < data.items.length; i++) {
                    var text = data.items[i].name;
                    var value = data.items[i].id;
                    userdropdownlist.append("<option value=" + value +">"+text+"</option>"); //为Select追加一个Option(下拉项)
                }; 

              
                $("#UserId option[value=" + userid+"]").attr('selected', 'true');
                $("#div-user").find("#UserId").selectpicker('refresh'); 
            });

        });

     
        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();